package com.chance.cc.crawler.development.bootstrap.kuaihe;

import com.alibaba.fastjson.JSONObject;
import com.chance.cc.crawler.core.downloader.*;
import com.chance.cc.crawler.core.downloader.http.HttpClientDownloader;
import com.chance.cc.crawler.core.downloader.http.HttpClientGenerator;
import com.chance.cc.crawler.core.downloader.proxy.Proxy;
import com.chance.cc.crawler.core.downloader.proxy.SiteConfigProxyProvider;
import groovy.util.logging.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import static com.chance.cc.crawler.core.downloader.HttpRequestBody.ContentType.FORM;
import static com.chance.cc.crawler.core.script.crawler.CrawlerScript.$_type;

/**
 * @Author Zhao.Hhuan
 * @Date Create in 2021/1/22 10:43
 * @Description
 *      1919吃喝多线程测试
 **/
@Slf4j
public class KuaiHeHttpTest {
    public static Logger logger = LoggerFactory.getLogger(KuaiHeHttpTest.class);

    private static Proxy proxy = new Proxy();
    static {
        //代理配置
        proxy.setHost("http-dyn.abuyun.com");
        proxy.setPort(9020);
        proxy.setUsername("H5168QRFNIU3804D");
        proxy.setPassword("5F6B3610BB719FAA");
    }

    public static void main(String[] args) {

        HttpConfig httpConfig = HttpConfig.me("test");
//        httpConfig.setProxy(proxy);
//        httpConfig.setUseProxy(true);

        HttpClientDownloader downloader = new HttpClientDownloader();
        downloader.setClientGenerator(new HttpClientGenerator());
        downloader.setProxyProvider(new SiteConfigProxyProvider());

        for (int i=1;i<=1;i++){
            new Thread(new GoodsDetail(downloader,httpConfig),"thread_"+i).start();
        }
    }

    static class TestRun implements Runnable{

        HttpClientDownloader downloader;
        HttpConfig httpConfig;

        public TestRun(HttpClientDownloader downloader, HttpConfig httpConfig) {
            this.downloader = downloader;
            this.httpConfig = httpConfig;
        }

        @Override
        public void run() {
            String url = "https://kuaihe-720.1919.cn/product/detail.do?addressLat=31.258734&addressLon=121.595218&cityId=289&depotId=358847913377927168&lat=31.258734&locationCityId=289&locationProvinceId=289&provinceId=289";
            HttpRequest httpRequest = new HttpRequest();
            httpRequest.setMethod(HttpConstant.Method.POST);
            httpRequest.addHeader("Host","kuaihe-720.1919.cn");
            httpRequest.addHeader("User-Agent","okhttp/3.12.1");
            String depotId = "358847913377927168";
            String id = "776179348441948160";
            try {
                httpRequest.setRequestBody(HttpRequestBody.custom(("data={\"depotId\":"+depotId+",\"id\":"+id+"}").getBytes("UTF-8"),FORM,"UTF-8"));
            } catch (UnsupportedEncodingException e) {
                logger.error(e.getMessage());
            }

            for(int i=0;i<1;i++){
                httpRequest.setUrl(String.format(url,Math.random()));
                HttpPage httpPage = downloader.download(httpRequest, httpConfig);
                System.out.println(httpPage);

                System.out.println( httpPage.getJson().jsonPath($_type + ".data").get());

            }
        }
    }

    //40个线程
    static class GoodsMoudles implements Runnable{

        HttpClientDownloader downloader;
        HttpConfig httpConfig;

        public GoodsMoudles(HttpClientDownloader downloader, HttpConfig httpConfig) {
            this.downloader = downloader;
            this.httpConfig = httpConfig;
        }

        @Override
        public void run() {
            String url = "https://kuaihe-720.1919.cn/sys/init.do";
            HttpRequest httpRequest = new HttpRequest();
            httpRequest.setMethod(HttpConstant.Method.POST);
            httpRequest.addHeader("Host","kuaihe-720.1919.cn");
            httpRequest.addHeader("User-Agent","okhttp/3.12.1");
            try {
                httpRequest.setRequestBody(HttpRequestBody.custom(("data=").getBytes("UTF-8"),FORM,"UTF-8"));
            } catch (UnsupportedEncodingException e) {
                logger.error(e.getMessage());
            }

            int count = 1;
            for(int i=0;i<count;i++){
                httpRequest.setUrl(String.format(url,Math.random()));
                HttpPage httpPage = downloader.download(httpRequest, httpConfig);

                List<String> all = new ArrayList<>();
                try{
                    all = httpPage.getJson().jsonPath($_type + ".data.categories").all();
                }catch (Exception e){
                    count = 5;
                    logger.error("down load page is error!");
                }

                if(i == 4){
                    logger.error("can not run!");
                }

                for (String data : all) {
                    JSONObject jsonObject = JSONObject.parseObject(data);
                    String categoryId = jsonObject.getString("id");
                    String categoryTitle = jsonObject.getString("title");
                    System.out.println("{categoryTitle : "+categoryTitle + ",categoryId : " + categoryId +"}");
                }
            }
        }
    }

    static class GoodsFilterInfo implements Runnable{

        HttpClientDownloader downloader;
        HttpConfig httpConfig;

        public GoodsFilterInfo(HttpClientDownloader downloader, HttpConfig httpConfig) {
            this.downloader = downloader;
            this.httpConfig = httpConfig;
        }

        @Override
        public void run() {
            String url = "https://kuaihe-720.1919.cn/product/searchList.do?cityId=289&depotId=358847913377927168";
            HttpRequest httpRequest = new HttpRequest();
            httpRequest.setMethod(HttpConstant.Method.POST);
            httpRequest.addHeader("Host","kuaihe-720.1919.cn");
            httpRequest.addHeader("User-Agent","okhttp/3.12.1");
            String categoryId = "337063315824054274";
            try {
                httpRequest.setRequestBody(HttpRequestBody.custom(("data={\"categoryId\":"+categoryId+"}").getBytes("UTF-8"),FORM,"UTF-8"));
            } catch (UnsupportedEncodingException e) {
                logger.error(e.getMessage());
            }


            for(int i=0;i<1;i++){
                httpRequest.setUrl(String.format(url,Math.random()));
                HttpPage httpPage = downloader.download(httpRequest, httpConfig);

                System.out.println("filters msg is:" + httpPage.getJson().jsonPath($_type + ".data").get());
            }
        }
    }


    static class GoodsIdList implements Runnable{

        HttpClientDownloader downloader;
        HttpConfig httpConfig;

        public GoodsIdList(HttpClientDownloader downloader, HttpConfig httpConfig) {
            this.downloader = downloader;
            this.httpConfig = httpConfig;
        }

        @Override
        public void run() {
            String url = "https://kuaihe-720.1919.cn/product/categoryProductId.do?addressLat=31.258492&addressLon=121.595534&cityId=289&depotId=358847913377927168";
            HttpRequest httpRequest = new HttpRequest();
            httpRequest.setMethod(HttpConstant.Method.POST);
            httpRequest.addHeader("Host","kuaihe-720.1919.cn");
            httpRequest.addHeader("User-Agent","okhttp/3.12.1");
            String page = "0";
            String categoryId = "337063315819859969";
            try {
                httpRequest.setRequestBody(HttpRequestBody.custom(("data={" +
                        "  \"depotId\": 358847913377927168," +
                        "  \"pageSize\": 30," +
                        "  \"sort\": \"SALES_VOLUME_DESC\"" +
                        "  \"page\": "+page+",\n" +
                        "  \"categoryId\": "+categoryId+",\n" +
                        "  \"customFlag\": false\n" +
                        "}").getBytes("UTF-8"),FORM,"UTF-8"));
            } catch (UnsupportedEncodingException e) {
                logger.error(e.getMessage());
            }

            for(int i=0;i<1;i++){
                httpRequest.setUrl(String.format(url,Math.random()));
                HttpPage httpPage = downloader.download(httpRequest, httpConfig);
                System.out.println(httpPage);

                List<String> all = httpPage.getJson().jsonPath($_type + ".data.products").all();
                for (String data : all) {
                    JSONObject jsonObject = JSONObject.parseObject(data);
                    String goodsId = jsonObject.getString("id");
                    String goodsName = jsonObject.getString("name");
                    System.out.println("{goodsName : "+goodsName + "，goodsId : " + goodsId+"}");
                }
            }
        }
    }

    static class GoodsDetail implements Runnable {

        HttpClientDownloader downloader;
        HttpConfig httpConfig;

        public GoodsDetail(HttpClientDownloader downloader, HttpConfig httpConfig) {
            this.downloader = downloader;
            this.httpConfig = httpConfig;
        }

        @Override
        public void run() {
            String url = "https://kuaihe-720.1919.cn/product/detail.do?addressLat=31.258525&addressLon=121.595488&cityId=289&depotId=358847913377927168&provinceId=289";
            HttpRequest httpRequest = new HttpRequest();
            httpRequest.setMethod(HttpConstant.Method.POST);
            httpRequest.addHeader("Host", "kuaihe-720.1919.cn");
            httpRequest.addHeader("User-Agent", "okhttp/3.12.1");
            String goodsId = "776179348441948160";
            try {
                httpRequest.setRequestBody(HttpRequestBody.custom(("data={\"depotId\":358847913377927168,\"id\":" + goodsId + "}").getBytes("UTF-8"), FORM, "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                logger.error(e.getMessage());
            }

            for (int i = 0; i < 1; i++) {
                httpRequest.setUrl(String.format(url, Math.random()));
                HttpPage httpPage = downloader.download(httpRequest, httpConfig);
                System.out.println(httpPage);

                System.out.println("goods detail is : " + httpPage.getJson().jsonPath($_type + ".data").get());
            }
        }
    }

    static class GoodsComments implements Runnable {

        HttpClientDownloader downloader;
        HttpConfig httpConfig;

        public GoodsComments(HttpClientDownloader downloader, HttpConfig httpConfig) {
            this.downloader = downloader;
            this.httpConfig = httpConfig;
        }

        @Override
        public void run() {
            String url = "https://kuaihe-720.1919.cn/product/evaluations.do?addressLat=31.258732&addressLon=121.595221";
            HttpRequest httpRequest = new HttpRequest();
            httpRequest.setMethod(HttpConstant.Method.POST);
            httpRequest.addHeader("Host", "kuaihe-720.1919.cn");
            httpRequest.addHeader("User-Agent", "okhttp/3.12.1");
            String goodsId = "776179348441948160";
            String page = "0";
            try {
                httpRequest.setRequestBody(HttpRequestBody.custom(("data={\"productId\":"+goodsId+",\"seckillItemId\":0,\"depotId\":358847913377927168,\"page\":"+page+"}").getBytes("UTF-8"), FORM, "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                logger.error(e.getMessage());
            }

            for (int i = 0; i < 1; i++) {
                httpRequest.setUrl(String.format(url, Math.random()));
                HttpPage httpPage = downloader.download(httpRequest, httpConfig);
                System.out.println(httpPage);

                List<String> all = httpPage.getJson().jsonPath($_type + ".data.page.items").all();
                for (String data : all) {
                    JSONObject jsonObject = JSONObject.parseObject(data);
                    String commentId = jsonObject.getString("id");
                    String author = jsonObject.getString("nickname");
                    String content = jsonObject.getString("evaluationContent");
                    String releaseTime = jsonObject.getString("evaluateTime");

                    System.out.println("{commentId : ,"+commentId+"author : "+author+",content : "+content+",releaseTime : "+releaseTime+"}" );
                }
            }
        }
    }
}
